package example.handler.impl;

import example.constant.PreConditionsCheck;
import example.constant.ResponseEnum;
import example.handler.MoneyHandler;
import example.handler.NormalUserHandler;
import example.model.NormalUser;
import example.param.NormalUserDeleteParam;
import example.param.NormalUserEditParam;
import example.param.NormalUserLoginParam;
import example.param.NormalUserSignInParam;
import example.service.NormalUserServie;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * Created by snksnk1091 on 2017/8/23.
 */
@Component
public class NormalUserHandlerImpl implements NormalUserHandler {

    @Autowired
    private NormalUserServie normalUserServie;
    @Autowired
    private MoneyHandler moneyHandler;

    public void normalUserSignIn(NormalUserSignInParam param) {
        PreConditionsCheck.checkArgument(normalUserServie.countNormalUserByName(param.getUserName()) > 0, ResponseEnum.USERNAME_ERROR);
        NormalUser normalUser = new NormalUser();
        normalUser.setUserName(param.getUserName());
        normalUser.setPassword(param.getPassword());
        normalUser.setEmail(param.getEmail());
        normalUser.setPhone(param.getPhone());
        normalUser.setMoeny(Double.parseDouble(param.getMoney()));
        normalUser.setIsMember(param.isMember());
        normalUser.setMemberCard(param.getMemberCard());
        normalUser.setIsdelete((byte)0);
        normalUser.setUpdateDate(new Date());
        normalUser.setCreateDate(new Date());
        normalUserServie.insertNormalUserByNU(normalUser);
        moneyHandler.createNewMoneyByNormalUser(normalUser);
    }

    public void normalUserEdit(NormalUserEditParam param) {
        NormalUser normalUser = normalUserServie.getNormalUserById(param.getId());
        PreConditionsCheck.checkArgument(normalUser == null, ResponseEnum.CHECK_VOID);
        PreConditionsCheck.checkArgument(normalUserServie.countNormalUserByName(param.getUserName()) > 0, ResponseEnum.USERNAME_ERROR);
        normalUser.setUserName(param.getUserName());
        normalUser.setEmail(param.getEmail());
        normalUser.setPhone(param.getPhone());
        normalUser.setMoeny(Double.parseDouble(param.getMoney()));
        normalUser.setIsMember(param.isMember());
        normalUser.setMemberCard(param.getMemberCard());
        normalUser.setUpdateDate(new Date());
        normalUserServie.updateNormalUserByNU(normalUser);
    }

    public void normalUserDelete(NormalUserDeleteParam param) {
        PreConditionsCheck.checkArgument(normalUserServie.countNormalUserByName(param.getUserName()) <= 0, ResponseEnum.CHECK_VOID);
        normalUserServie.deleteNormalUserByName(param.getUserName());
        moneyHandler.deleteMoneyByUserName(param.getUserName());
    }

    public NormalUser getNormalUser(int id) {
        PreConditionsCheck.checkArgument(normalUserServie.countNormalUserById(id) <= 0, ResponseEnum.CHECK_VOID);
        return normalUserServie.getNormalUserById(id);
    }

    public Object normalUserLogin(NormalUserLoginParam param) {
        PreConditionsCheck.checkArgument(normalUserServie.countNormalUserByName(param.getUserName()) <=0, ResponseEnum.CHECK_VOID);
        PreConditionsCheck.checkArgument(!normalUserServie.checkPassword(param.getUserName(), param.getPassword()), ResponseEnum.PASSWORDCHECK_ERROR);
        return ResponseEnum.SUCCESS;
    }
}
